package zuoye3;

public class Student extends Person {
    private int no;
    private double scoreEng;
    private double scoreMath;
    private double scoreCh;
    private transient double cachedAverageScore;
    private transient double cachedMaxScore;
    private transient double cachedMinScore;

    public Student(String name, int age, String sex, int no, double scoreEng, double scoreMath, double scoreCh) {
        super(name, age, sex);
        validateNo(no);
        validateScore(scoreEng);
        validateScore(scoreMath);
        validateScore(scoreCh);

        this.no = no;
        this.scoreEng = scoreEng;
        this.scoreMath = scoreMath;
        this.scoreCh = scoreCh;
    }

    private void validateNo(int no) {
        if (no <= 0) {
            throw new IllegalArgumentException("学号必须大于0");
        }
    }

    private void validateScore(double score) {
        if (score < 0 || score > 100) {
            throw new IllegalArgumentException("成绩必须在0到100之间");
        }
    }

    public double getAverageScore() {
        if (Double.isNaN(cachedAverageScore)) {
            cachedAverageScore = (scoreEng + scoreMath + scoreCh) / 3.0;
        }
        return cachedAverageScore;
    }

    public double getMaxScore() {
        if (Double.isNaN(cachedMaxScore)) {
            cachedMaxScore = Math.max(Math.max(scoreEng, scoreMath), scoreCh);
        }
        return cachedMaxScore;
    }

    public double getMinScore() {
        if (Double.isNaN(cachedMinScore)) {
            cachedMinScore = Math.min(Math.min(scoreEng, scoreMath), scoreCh);
        }
        return cachedMinScore;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + super.getName() + '\'' +
                ", age=" + super.getAge() +
                ", sex='" + super.getSex() + '\'' +
                ", no=" + no +
                ", scoreEng=" + scoreEng +
                ", scoreMath=" + scoreMath +
                ", scoreCh=" + scoreCh +
                ", averageScore=" + getAverageScore() +
                ", maxScore=" + getMaxScore() +
                ", minScore=" + getMinScore() +
                '}';
    }
}
